package com.ocom.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.human.EntityBizApply;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.company.EntityBizApplyExtendVo;
import com.ocom.company.dao.IDaoBizApply;
import com.ocom.company.service.BizApplyService;
import com.ocom.mysql.aspect.DataSourceAspect;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BizApplyServiceImpl extends JoinServiceImpl<IDaoBizApply, EntityBizApply> implements BizApplyService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean addApply(Long companyId, EntityBizApply entityBizApply) {
        return this.save(entityBizApply);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean updateApply(Long companyId, EntityBizApply entityBizApply) {
        UpdateWrapper<EntityBizApply> updateWrapper= new UpdateWrapper<>();
        updateWrapper.eq("business_id",entityBizApply.getBusinessId());
        if(null!=entityBizApply.getStatus()){
            updateWrapper.set("status",entityBizApply.getStatus());
        }
        if(null!=entityBizApply.getActivitiKey()){
            updateWrapper.set("activiti_key",entityBizApply.getActivitiKey());
        }
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityBizApply getApplyOne(Long companyId, EntityBizApply entityBizLeaveApply) {
        return  this.getOne(Wrappers.lambdaQuery(EntityBizApply.class).eq(EntityBizApply::getBusinessId,entityBizLeaveApply.getBusinessId()).last("limit 1"));
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityBizApplyExtendVo> getApplyListByPid(Long companyId, EntityBizApply apply) {
        JoinLambdaWrapper<EntityBizApply> queryWrapper =  new JoinLambdaWrapper<>(EntityBizApply.class);
        if(apply.getPId()!=null){
            queryWrapper.eq(EntityBizApply::getPId,apply.getPId());
        }
        if(apply.getApplyDeptId()!=null){
            queryWrapper.eq(EntityBizApply::getApplyDeptId,apply.getApplyDeptId());
        }
        queryWrapper.eq(EntityBizApply::getComId,apply.getComId());
        // 判断参数是否为空
        if (apply.getStatus() != null) {
            // 如果参数非空，添加查询条件
            queryWrapper.eq(EntityBizApply::getStatus, apply.getStatus());
        }
        queryWrapper.orderByDesc(EntityBizApply::getCreateTime);
        queryWrapper
                .leftJoin(EntityBizLeaveApply.class, EntityBizLeaveApply::getId, EntityBizApply::getBusinessId)
                .select(EntityBizLeaveApply::getStartTime,EntityBizLeaveApply::getEndTime
                ,EntityBizLeaveApply::getReason,EntityBizLeaveApply::getPic,EntityBizLeaveApply::getDays).end()
                .leftJoin(EntityPersonnelInfo.class, EntityPersonnelInfo::getPId, EntityBizApply::getPId)
                .selectAs((cb) -> {
                    cb.add(" personnel_info.p_name ","pName",false);
                    cb.add(" personnel_info.p_code ","pCode",false);
                    cb.add(" personnel_info.p_photo_url2 ","photo",false);
                    cb.add(" personnel_info.p_phone ","phone",false);
                })
                .end();
//        IPage<EntityBizApplyExtendVo> page = new Page<>();
//        page.setCurrent(apply.getPage());
//        page.setSize(apply.getSize());
//        return this.joinPage(page,queryWrapper,EntityBizApplyExtendVo.class);
        PageHelper.startPage(apply.getPage(), apply.getSize());
        List<EntityBizApplyExtendVo> bizApplyExtendVos =  this.joinList(queryWrapper,EntityBizApplyExtendVo.class);
        PageInfo<EntityBizApplyExtendVo> pageInfo=new PageInfo<>(bizApplyExtendVos);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean update(Long companyId, EntityBizApply entityBizApply) {
        return this.update(entityBizApply,Wrappers.lambdaQuery(EntityBizApply.class)
                .eq(EntityBizApply::getComId,entityBizApply.getComId())
                .eq(EntityBizApply::getBusinessId,entityBizApply.getBusinessId())
        );
    }
}
